package com.biostime.qdingding.modules.qd.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.biostime.qdingding.common.config.Global;
import com.biostime.qdingding.common.persistence.Page;
import com.biostime.qdingding.common.utils.StringUtils;
import com.biostime.qdingding.common.web.BaseController;
import com.biostime.qdingding.modules.qd.entity.Center;
import com.biostime.qdingding.modules.qd.service.CenterService;
import com.biostime.qdingding.modules.sys.entity.Area;
import com.biostime.qdingding.modules.sys.entity.User;
import com.biostime.qdingding.modules.sys.service.AreaService;
import com.biostime.qdingding.modules.sys.service.UserService;
import com.biostime.qdingding.modules.sys.utils.UserUtils;

/**
 * 早教中心Controller
 * @author king
 * @version 2015-11-05
 */
@Controller
@RequestMapping(value = "${adminPath}/qd/center")
public class CenterController extends BaseController {

	@Autowired
	private CenterService centerService;
	@Autowired
	private AreaService areaService;
	@Autowired
	private UserService userService;
	
	@ModelAttribute
	public Center get(@RequestParam(required=false) String id) {
		Center entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = centerService.get(id);
		}
		if (entity == null){
			entity = new Center();
		}
		return entity;
	}
	
	@RequestMapping("getlimitdate")
	public void getLimitDate(HttpServletRequest request,HttpServletResponse response){
		String siginDate =	request.getParameter("contractSignDate");
		String period = request.getParameter("contractPeriod");
		try {
			if(!StringUtils.isEmpty(siginDate) ){
				String[] date = siginDate.split("-");
				String limitDate = Integer.valueOf(date[0])+Integer.valueOf(period)+"-"+date[1]+"-"+date[2];
				response.getWriter().write(limitDate);
			}else{
				response.getWriter().write("参数为空,无法自动生成");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@RequiresPermissions("qd:center:view")
	@RequestMapping(value = {"list", ""})
	public String list(Center center, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Center> page = centerService.findPage(new Page<Center>(request, response), center); 
		model.addAttribute("page", page);
		return "modules/qd/centerList";
	}

	@RequiresPermissions("qd:center:view")
	@RequestMapping(value = "form")
	public String form(Center center, Model model) {
		List<Center> parentCenters = centerService.getParentsTree();
		
		Area province = new Area();
		province.setLayer(2);
		List<Area> provinceList = areaService.findList(province);
		
		List<Area> cityList = null;
		if(center.getProvince()!=null){
			Area city = new Area();
			city.setParent(new Area(center.getProvince().getId()));
			city.setLayer(3);
			cityList = areaService.findList(city);
		}
		
		List<Area> areaList = null;
		if(center.getProvince()!=null){
			Area area = new Area();
			area.setParent(new Area(center.getCity().getId()));
			area.setLayer(4);
			areaList = areaService.findList(area);
		}
		
		model.addAttribute("provinceList", provinceList);
		model.addAttribute("cityList", cityList);
		model.addAttribute("areaList", areaList);
		model.addAttribute("pareCenters", parentCenters);
		model.addAttribute("center", center);
		return "modules/qd/centerForm";
	}

	@RequiresPermissions(value={"qd:center:edit","qd:center:add"}, logical=Logical.OR)
	@RequestMapping(value = "save")
	public String save(Center center, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, center)){
			return form(center, model);
		}
		centerService.save(center);
		addMessage(redirectAttributes, "保存成功");
		return "redirect:"+Global.getAdminPath()+"/qd/center/?repage";
	}
	
	@RequiresPermissions("qd:center:edit")
	@RequestMapping(value = "delete")
	public String delete(@RequestParam(value="ids",required=false) String idsRequest, Center center, RedirectAttributes redirectAttributes) {
		//批量删除
		if(!StringUtils.isEmpty(idsRequest)){
			String[] ids = idsRequest.split(",");
			for(String id : ids){
				centerService.delete(new Center(id));
			}
		}else{
			centerService.delete(center);
		}
		
		addMessage(redirectAttributes, "删除成功");
		return "redirect:"+Global.getAdminPath()+"/qd/center/?repage";
	}
	
	/***
	 * 切换早教中心
	 * @return
	 */
	//@RequiresPermissions("qd:center:view")
	@RequestMapping(value = "change")
	public String change(Center center,HttpServletRequest request, RedirectAttributes redirectAttributes) {
		
		if(StringUtils.isNotBlank(center.getId())){
			User user = UserUtils.getUser();
			user.setCenter(center);
			userService.save(user);
		}
		
		return "redirect:"+request.getParameter("url");
	}
}